package com.hnkywl.sts.service.jhgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.base.SjzdzDao;
import com.hnkywl.sts.dao.jhgl.CgxyDao;
import com.hnkywl.sts.dao.jhgl.SfxyDao;
import com.hnkywl.sts.dao.jhgl.XsxyDao;
import com.hnkywl.sts.dao.jhgl.XyDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.entity.jhgl.Cgxy;
import com.hnkywl.sts.entity.jhgl.Sfxy;
import com.hnkywl.sts.entity.jhgl.Xsxy;
import com.hnkywl.sts.entity.jhgl.Xy;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-2
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
@Service
public class XyService {
    private XyDao xyDao;

    private CgxyDao cgxyDao;

    private XsxyDao xsxyDao;

    private SfxyDao sfxyDao;

    private BmService bmService;

    private GsjgDao gsjgDao;

    private SjzdzDao sjzdzDao;

    /**
     * 查询所有协议
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllXy(String xyhm, Long ssgsId, Long gysId, Long khId, String spzt, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Xy> xyPage = this.xyDao.findXyList(xyhm, ssgsId, gysId, khId, spzt, pageNo, pageSize);
        String xyJson = gson.toJson(xyPage.getResult());
//        String xyJson = BaseUtils.fetchCollectionToJson(xyPage.getResult(), "id", "xyhm","kssj","jssj", "nxyl", "yxyl", "bz","type","xyzt","processInstanceId");
        return "{\"total\":" + xyPage.getTotalCount() + ",\"rows\":" + xyJson + "}";
    }

    /**
     * 查询所有采购协议
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllCgXy(String xyhm, Long ssgsId, Long gysId, Long khId, String spzt, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Cgxy> xyPage = this.cgxyDao.findXyList(xyhm, ssgsId, gysId, khId, spzt, pageNo, pageSize);
        String xyJson = gson.toJson(xyPage.getResult());
//        String xyJson = BaseUtils.fetchCollectionToJson(xyPage.getResult(), "id", "xyhm","kssj","jssj","gys.glqy.id","gys.glqy.qc", "nxyl", "yxyl", "bz","type","xyzt","processInstanceId");
        return "{\"total\":" + xyPage.getTotalCount() + ",\"rows\":" + xyJson + "}";
    }

    /**
     * 首页查询待提交的采购协议数
     *
     * @return
     */
    public String findAllYbcCgXy() {
        Integer xyNum = 0;
        try {
            List xyList = this.xyDao.findXyList("ybc","'cgxy','sfxy'");
            return xyList.get(0).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xyNum.toString();
    }

    /**
     * 首页查询待提交的协议数
     *
     * @return
     */
    public String findAllYbcXsXy() {
        Integer xyNum = 0;
        try {
            List xyList = this.xyDao.findXyList("ybc","xsxy");
            return xyList.get(0).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xyNum.toString();
    }

    /**
     * 查询所有采购协议
     *
     * @param types
     * @param ssgsId
     * @param spzt
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllXy(List<String> types, String tplx,String tcgs, Long ssgsId, String spzt, int pageNo, int pageSize) {
        Page<Map> page = xyDao.findXyList(types, tplx, tcgs, ssgsId, spzt, pageNo, pageSize);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + json + "}";
    }

    /**
     * 查询所有销售协议
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllXsXy(String xyhm, Long ssgsId, Long gysId, Long khId, String spzt, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Xsxy> xyPage = this.xsxyDao.findXyList(xyhm, ssgsId, gysId, khId, spzt, pageNo, pageSize);
        String xyJson = gson.toJson(xyPage.getResult());
        //String xyJson = BaseUtils.fetchCollectionToJson(xyPage.getResult(), "id", "xyhm","kssj","jssj","kh.glqy.id","kh.glqy.qc", "nxyl", "yxyl", "bz","type","xyzt","processInstanceId");
        return "{\"total\":" + xyPage.getTotalCount() + ",\"rows\":" + xyJson + "}";
    }

    /**
     * 查询所有三方协议
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String findAllSfXy(String xyhm, Long ssgsId, Long gysId, Long khId, String spzt, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page<Sfxy> xyPage = this.sfxyDao.findXyList(xyhm, ssgsId, gysId, khId, spzt, pageNo, pageSize);
        String xyJson = gson.toJson(xyPage.getResult());
        //String xyJson = BaseUtils.fetchCollectionToJson(xyPage.getResult(), "id", "xyhm","kssj","jssj","gys.glqy.id","gys.glqy.qc","kh.glqy.id","kh.glqy.qc",  "nxyl", "yxyl", "bz","type","xyzt","processInstanceId");
        return "{\"total\":" + xyPage.getTotalCount() + ",\"rows\":" + xyJson + "}";
    }

    public String findAllSimpleXsXy(int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page xyPage = this.xyDao.findAllSimpleXsXy(pageNo, pageSize);
        String xyJson = gson.toJson(xyPage.getResult());
        return "{\"total\":" + xyPage.getTotalCount() + ",\"rows\":" + xyJson + "}";
    }

    /**
     * 查询协议明细
     *
     * @param xyId 协议ID
     * @return
     */
    public Xy findXyDetail(Long xyId) {
        return this.xyDao.get(xyId);
    }

    /**
     * 查询协议明细
     *
     * @param processInstanceId 协议ID
     * @return
     */
    @Transactional
    public Xy findXyDetailByProcessInstance(String processInstanceId) {
        return this.xyDao.findUniqueByProperty("processInstanceId", processInstanceId);
    }

    /**
     * 保存采购协议信息
     *
     * @param xy 协议
     * @return
     */
    @Transactional
    public void saveXy(Xy xy) throws SQLException {
        xy.setXyhm(this.bmService.getXybm(xy.getClass().getSimpleName()));
        initXy(xy);
        if (xy.getJyms() != null && xy.getJyms().getTcgs() != null) {
            xy.getJyms().setTcgs(gsjgDao.get(xy.getJyms().getTcgs().getId()));
        }
        if (xy.getJyms() != null && xy.getJyms().getTcbm() != null) {
            xy.getJyms().setTcbm(gsjgDao.get(xy.getJyms().getTcbm().getId()));
        }
        if (xy.getJyms() != null) {
            xy.getJyms().setXy(xy);
        }
        xy.setZdr(YgService.getCurrentYg());
        xy.setZdsj(new Date());
        xy.setCreateUser(YgService.getCurrentYg());
        xy.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xy.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xy.setDateCreated(new Date());
        xy.setLastUpdated(xy.getDateCreated());
        this.xyDao.save(xy);
    }

    /**
     * 保存采购协议信息
     *
     * @param xy 协议
     * @return
     */
    @Transactional
    public void editXy(Xy xy, String xyhm) {
        initXy(xy);
        xy.setXyhm(xyhm);
        if (xy.getJyms() != null && xy.getJyms().getTcgs() != null && xy.getJyms().getTcgs().getId() != null) {
            xy.getJyms().setTcgs(gsjgDao.get(xy.getJyms().getTcgs().getId()));
        }
        if (xy.getJyms() != null && xy.getJyms().getTcbm() != null && xy.getJyms().getTcbm().getId() != null) {
            xy.getJyms().setTcbm(gsjgDao.get(xy.getJyms().getTcbm().getId()));
        }
        xy.setLastUpdated(new Date());
        this.xyDao.save(xy);
    }


    /**
     * 删除协议信息
     *
     * @param id 协议ID
     * @return
     */
    @Transactional
    public Xy deleteXy(Long id) {
        Xy xy = this.xyDao.get(id);
        xy.setLastUpdated(new Date());
        xy.setDeleted(1);
        this.xyDao.save(xy);
        return xy;
    }


    public void initXy(Xy xy) {
        if (xy != null && xy.getJyms() != null && xy.getJyms().getTcfs() != null && xy.getJyms().getTcfs().getId() == null)
            xy.setJyms(null);
        if (xy != null && xy.getJyms() != null && xy.getJyms().getTcgs() != null && xy.getJyms().getTcgs().getId() == null)
            xy.getJyms().setTcgs(null);
        if (xy != null && xy.getJyms() != null && xy.getJyms().getTcbm() != null && xy.getJyms().getTcbm().getId() == null)
            xy.getJyms().setTcbm(null);
        if (xy != null && xy.getJyms() != null && xy.getJyms().getFklx() != null && xy.getJyms().getFklx().getId() == null)
            xy.getJyms().setFklx(null);
        if (xy != null && xy.getJyms() != null && xy.getJyms().getTcfs() != null && xy.getJyms().getTcfs().getBm() == null)
            xy.getJyms().setTcfs(sjzdzDao.get(xy.getJyms().getTcfs().getId()));

    }

    /**
     * 履约协议信息
     *
     * @param id 协议ID
     * @return
     */
    @Transactional
    public void agreeXy(Long id, Yg yg) {
        Xy xy = this.xyDao.get(id);
        xy.setSfly(true);

        xy.setLyr(yg);
        xy.setLysj(new Date());

        xy.setLastUpdated(xy.getLysj());
        this.xyDao.save(xy);
    }

    /**
     * 终止协议信息
     *
     * @param id   协议ID
     * @param zzyy 终止原因
     * @return
     */
    @Transactional
    public void stopXy(Long id, Yg yg, String zzyy) {
        Xy xy = this.xyDao.get(id);
        xy.setSfqzzz(true);
        xy.setZzr(yg);
        xy.setZzsj(new Date());
        xy.setZzyy(zzyy);
        xy.setXyzt("yzz");
        xy.setLastUpdated(xy.getZzsj());
        this.xyDao.save(xy);
    }


    @Autowired
    public void setXyDao(XyDao xyDao) {
        this.xyDao = xyDao;
    }

    @Autowired
    public void setCgxyDao(CgxyDao cgxyDao) {
        this.cgxyDao = cgxyDao;
    }

    @Autowired
    public void setXsxyDao(XsxyDao xsxyDao) {
        this.xsxyDao = xsxyDao;
    }

    @Autowired
    public void setSfxyDao(SfxyDao sfxyDao) {
        this.sfxyDao = sfxyDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }

    @Autowired
    public void setSjzdzDao(SjzdzDao sjzdzDao) {
        this.sjzdzDao = sjzdzDao;
    }
}
